package action.plan;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.security.context.SecurityContext;
import org.springframework.security.context.SecurityContextHolder;

import persistence.model.TdfkfaBasicInvest;
import persistence.model.TdfkfaBasicScxmgk;
import persistence.model.TdfkfaBasicXmqntdphlx;
import persistence.model.TdfkfaBasicXmqtdlyxz;
import persistence.model.TdfkfaBasicYqfkmj;
import persistence.model.TdfkfaComposeUnit;
import persistence.model.TdfkfaMain;
import presentation.extendmodel.PlanDetailModel;
import presentation.extendmodel.TdfkfaMainEx;
import service.PlanService;
import service.UserService;

import com.opensymphony.xwork2.ActionSupport;

import constant.DictConstant;
import constant.ServiceConstants;

public class SubmitPlanAction extends ActionSupport implements ServletRequestAware, SessionAware {

    private static final long serialVersionUID = 1L;

    private Map<String, Object> session;

    private PlanService planService;

    private UserService userService;

    private TdfkfaMain main;

    private TdfkfaBasicXmqntdphlx phlx;

    private TdfkfaBasicScxmgk xmgk;

    private TdfkfaBasicYqfkmj fkmj;

    private TdfkfaBasicXmqtdlyxz lyxz;

    private TdfkfaBasicInvest fktz;
    
    private TdfkfaComposeUnit unit;

    private PlanDetailModel planDetail;

    private String returnCode;

    private String returnDetail;

    private String planId;

    private String[] nameList;

    private List<String> submitedDxtpUrlList;

    private HttpServletRequest request;

    public String getDetailData() throws Exception {

        // 基本信息
        TdfkfaMainEx tdfkfaMainEx = (TdfkfaMainEx) session.get("TempStore_tdfkfaMainEx");

        // 项目概况
        TdfkfaBasicScxmgk tdfkfaBasicScxmgk = (TdfkfaBasicScxmgk) session.get("TempStore_tdfkfaBasicScxmgk");

        // 编制单位
        TdfkfaComposeUnit tdfkfaComposeUnit = (TdfkfaComposeUnit) session.get("TempStore_tdfkfaComposeUnit");

        // 复垦投资
        TdfkfaBasicInvest tdfkfaBasicInvest = (TdfkfaBasicInvest) session.get("TempStore_tdfkfaBasicInvest");

        // 利用现状
        TdfkfaBasicXmqtdlyxz tdfkfaBasicXmqtdlyxz = (TdfkfaBasicXmqtdlyxz) session.get("TempStore_tdfkfaBasicXmqtdlyxz");

        // 破坏类型
        TdfkfaBasicXmqntdphlx tdfkfaBasicXmqntdphlx = (TdfkfaBasicXmqntdphlx) session.get("TempStore_tdfkfaBasicXmqntdphlx");

        // 预期复垦面积
        TdfkfaBasicYqfkmj tdfkfaBasicYqfkmj = (TdfkfaBasicYqfkmj) session.get("TempStore_tdfkfaBasicYqfkmj");

        planDetail = new PlanDetailModel();

        planDetail.setMain(tdfkfaMainEx);

        planDetail.setInvest(tdfkfaBasicInvest);

        planDetail.setScxmgk(tdfkfaBasicScxmgk);

        planDetail.setXmqntdphlx(tdfkfaBasicXmqntdphlx);

        planDetail.setXmqtdlyxz(tdfkfaBasicXmqtdlyxz);

        planDetail.setYqfkmj(tdfkfaBasicYqfkmj);

        planDetail.setUnit(tdfkfaComposeUnit);

        return SUCCESS;
    }

    public String savePlanBasic() throws Exception {

        SecurityContext ctx = SecurityContextHolder.getContext();
        String username = ctx.getAuthentication().getName();
        TdfkfaMainEx uPlan = planService.getOwnPlan(username);

        main.setPrjFaztcode(new Short(DictConstant.FAZT_EDITING));
        Integer id = planService.savePlanBasic(main);
        if (id != null) {
            if (uPlan != null && !id.equals(uPlan.getPrjPkid())) {
                returnCode = "failure";
                returnDetail = "保存失败，请勿重复提交方案";
                planService.deletePlan(id);
            } else {
                int i = userService.savePlanToUser(username, id, main.getPrjName());
                if (i == 1) {
                    planId = id.toString();
                    returnCode = "success";
                } else {
                    returnCode = "failure";
                    returnDetail = "保存失败";
                }
            }
        } else {
            returnCode = "failure";
            returnDetail = "保存失败";
        }

        return SUCCESS;
    }

    public String savePlanPhlx() throws Exception {

        int ret = planService.savePlanPhlx(phlx);
        if (ret == ServiceConstants.SUCCESS) {
            returnCode = "success";
        } else {
            returnCode = "failure";
            returnDetail = "保存失败";
        }

        return SUCCESS;
    }

    public String savePlanXmgk() throws Exception {

        int ret = planService.savePlanXmgk(xmgk);
        if (ret == ServiceConstants.SUCCESS) {
            returnCode = "success";
        } else {
            returnCode = "failure";
            returnDetail = "保存失败";
        }
        return SUCCESS;
    }

    public String savePlanFkmj() throws Exception {

        int ret = planService.savePlanFkmj(fkmj);
        if (ret == ServiceConstants.SUCCESS) {
            returnCode = "success";
        } else {
            returnCode = "failure";
            returnDetail = "保存失败";
        }
        return SUCCESS;
    }

    public String savePlanLyxz() throws Exception {

        int ret = planService.savePlanLyxz(lyxz);
        if (ret == ServiceConstants.SUCCESS) {
            returnCode = "success";
        } else {
            returnCode = "failure";
            returnDetail = "保存失败";
        }
        return SUCCESS;
    }

    public String savePlanFktz() throws Exception {

        int ret = planService.savePlanFktz(fktz);
        if (ret == ServiceConstants.SUCCESS) {
            returnCode = "success";
        } else {
            returnCode = "failure";
            returnDetail = "保存失败";
        }
        return SUCCESS;
    }

    public String savePlanUnit() throws Exception {

        int ret = planService.savePlanUnit(unit);
        if (ret == ServiceConstants.SUCCESS) {
            returnCode = "success";
        } else {
            returnCode = "failure";
            returnDetail = "保存失败";
        }
        return SUCCESS;
    }

    public String saveDxtpInfo() throws Exception {

        String subPath = DigestUtils.shaHex(planId);
        String path = getUrl(request).concat("/planFile/dxtp/").concat(subPath).concat("/");
        if (nameList != null) {
            submitedDxtpUrlList = new ArrayList<String>();
            for (String name : nameList) {
                String url = path.concat(name);
                submitedDxtpUrlList.add(url);
            }
            int ret = planService.saveDxtp(new Integer(planId), submitedDxtpUrlList);
            if (ret == ServiceConstants.SUCCESS) {
                returnCode = "success";
            } else {
                returnCode = "failure";
                returnDetail = "保存失败";
            }
        }

        return SUCCESS;
    }

    public void setSession(Map<String, Object> session) {
        this.session = session;
    }

    public void setPlanService(PlanService planService) {
        this.planService = planService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public PlanDetailModel getPlanDetail() {
        return planDetail;
    }

    public void setPlanDetail(PlanDetailModel planDetail) {
        this.planDetail = planDetail;
    }

    public TdfkfaMain getMain() {
        return main;
    }

    public void setMain(TdfkfaMain main) {
        this.main = main;
    }

    public TdfkfaBasicXmqntdphlx getPhlx() {
        return phlx;
    }

    public void setPhlx(TdfkfaBasicXmqntdphlx phlx) {
        this.phlx = phlx;
    }

    public TdfkfaBasicScxmgk getXmgk() {
        return xmgk;
    }

    public void setXmgk(TdfkfaBasicScxmgk xmgk) {
        this.xmgk = xmgk;
    }

    public TdfkfaBasicYqfkmj getFkmj() {
        return fkmj;
    }

    public void setFkmj(TdfkfaBasicYqfkmj fkmj) {
        this.fkmj = fkmj;
    }

    public TdfkfaBasicXmqtdlyxz getLyxz() {
        return lyxz;
    }

    public void setLyxz(TdfkfaBasicXmqtdlyxz lyxz) {
        this.lyxz = lyxz;
    }

    public TdfkfaBasicInvest getFktz() {
        return fktz;
    }

    public void setFktz(TdfkfaBasicInvest fktz) {
        this.fktz = fktz;
    }

    public TdfkfaComposeUnit getUnit() {
        return unit;
    }

    public void setUnit(TdfkfaComposeUnit unit) {
        this.unit = unit;
    }

    public String getReturnCode() {
        return returnCode;
    }

    public String getReturnDetail() {
        return returnDetail;
    }

    public void setPlanId(String planId) {
        this.planId = planId;
    }

    public String getPlanId() {
        return planId;
    }

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public String[] getNameList() {
        return nameList;
    }

    public void setNameList(String[] nameList) {
        this.nameList = nameList;
    }

    public List<String> getSubmitedDxtpUrlList() {
        return submitedDxtpUrlList;
    }

    public void setSubmitedDxtpUrlList(List<String> submitedDxtpUrlList) {
        this.submitedDxtpUrlList = submitedDxtpUrlList;
    }

    private String getUrl(HttpServletRequest request) {
        StringBuffer sb = new StringBuffer();
        sb.append(request.getContextPath()).append("/");
        return sb.toString();
    }
}
